package com.hg.onlineshop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hg.onlineshop.entity.Product;
import com.hg.onlineshop.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductMapper productMapper;

    @GetMapping("/list")
    public List<Product> findProductByCondition(Product product) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        String categoryId = product.getCategoryId();
        String productName = product.getProductName();
        queryWrapper.eq("del_status", "0");
        if (!StringUtils.isEmpty(categoryId)) {
            queryWrapper.eq("category_id", categoryId);
        }
        if (!StringUtils.isEmpty(productName)) {
            queryWrapper.like("product_name", productName);
        }
        List<Product> products = productMapper.selectList(queryWrapper);
        return products;
    }

    @GetMapping("/all")
    public List<Product> findAllProduct() {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_status", "0");
        List<Product> products = productMapper.selectList(queryWrapper);
        return products;
    }

    @GetMapping("/single/{id}")
    public Product getProduct(@PathVariable("id")Integer id) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Product queryProduct = productMapper.selectOne(queryWrapper);
        return queryProduct;
    }

}
